package com.cuijq.counterv6.controller;

import com.cuijq.counterv5.controller.IUserController;
import com.cuijq.counterv5.controller.MetricsCollector;
import com.cuijq.counterv5.controller.UserController;
import com.cuijq.counterv5.entity.RequestInfo;
import com.cuijq.counterv5.entity.UserVo;

/**
 * @author by CuiJQ
 * @Classname UserControllerProxy
 * @Description UserControllerProxy Description
 * @Date 2021/5/21 19:16
 */
public class UserControllerProxy implements IUserController {


  private com.cuijq.counterv5.controller.MetricsCollector metricsCollector;
  private com.cuijq.counterv5.controller.UserController userController;

  public UserControllerProxy(UserController userController) {
    this.userController = userController;
    this.metricsCollector = new MetricsCollector();
  }

  @Override
  public UserVo login(String telephone, String password) {
    long startTimestamp = System
        .currentTimeMillis(); // 委托
    UserVo userVo = userController.login(telephone, password);
    long endTimeStamp = System.currentTimeMillis();
    long responseTime = endTimeStamp - startTimestamp;
    RequestInfo requestInfo = new RequestInfo("login", responseTime, startTimestamp);
    metricsCollector.recordRequest(requestInfo);
    return userVo;
  }

  @Override
  public UserVo register(String telephone, String password) {
    long startTimestamp = System.currentTimeMillis();
    UserVo userVo = userController.register(telephone, password);
    long endTimeStamp = System.currentTimeMillis();
    long responseTime = endTimeStamp - startTimestamp;
    RequestInfo requestInfo = new RequestInfo("register", responseTime, startTimestamp);
    metricsCollector.recordRequest(requestInfo);
    return userVo;
  }
}